#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <easyx.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
using namespace std;

#define NUMS_BALL 166
typedef struct ball
{
	int x;
	int y;
	int vx;
	int vy;
	COLORREF color;
}ball;


int main()
{

	initgraph(1000, 800);
	setorigin(500, 400);
	setaspectratio(1, -1);

	setbkcolor(WHITE);
	setlinecolor(BLACK);
	cleardevice();

	srand((unsigned int)time(NULL));
	int r = 10;
	int m = 1000;  //781 - 390  //
	int n = 800;  //581 - 290

	ball* balls = (ball*)malloc(sizeof(ball) * NUMS_BALL);
	for (int i = 0; i < NUMS_BALL; i++)
	{

		balls[i].x = rand() % (m - 2 * r + 1) - (m / 2 - r);
		balls[i].y = rand() % (n - 2 * r + 1) - (n / 2 - r);

		balls[i].vx = rand() % 11 - 5;
		balls[i].vy = rand() % 11 - 5;

		balls[i].color = HSVtoRGB(rand() % 360, 0.8, 0.9);

	}

	BeginBatchDraw();
	while (1)
	{
		cleardevice();
		
		for (int i = 0; i < NUMS_BALL; i++)
		{
			setfillcolor(balls[i].color);
			fillcircle(balls[i].x, balls[i].y, r);
		}

		FlushBatchDraw();
		Sleep(40);

		for (int i = 0; i < NUMS_BALL; i++)
		{
			if (balls[i].x >= (m / 2 - r) || balls[i].x <= -(m / 2 - r))
			{
				balls[i].vx = -balls[i].vx;
			}
			if (balls[i].y >= (n / 2 - r) || balls[i].y <= -(n / 2 - r))
			{
				balls[i].vy = -balls[i].vy;
			}

			balls[i].x += balls[i].vx;
			balls[i].y += balls[i].vy;

		}
		
	}

	EndBatchDraw();

	free(balls);
	getchar();
	closegraph();
	return 0;
}
